//
//  BasePopView.m
//  智云网校
//
//  Created by 毕武侠 on 2017/5/19.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "BasePopView.h"

@interface BasePopView ()

@property (nonatomic, strong) UIView *backView;

@end

@implementation BasePopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigre];
    }
    return self;
}

- (void)setupConfigre {
    self.didSignal = [RACSubject subject];
    
    [self addSubview:self.backView];
}

- (void)touchBackView:(id)sender{
    [self setShowView:NO animation:YES];
}

- (void)setShowView:(BOOL)show animation:(BOOL)animation {
    self.hidden = !show;
    
    if (show == YES && animation == YES) {
        [self setSubViewAnimation];
    }
}

- (void)setSubViewAnimation {}

- (void)addDownAnimationToView:(UIView *)view {
    [view.layer pop_removeAnimationForKey:@"positiony"];
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anSpring.fromValue = @(-view.height + self.top);
    anSpring.toValue = @(self.top);
    anSpring.springBounciness = 10.0f;
    [view.layer pop_addAnimation:anSpring forKey:@"positiony"];
}

#pragma mark - ViewSignalProtocol
- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    if ([str isEqualToString:@"select"]) {
        return self.didSignal;
    } else {
        return [RACSignal empty];
    }
}

#pragma mark - getter
- (UIView *)backView {
    if (_backView == nil) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _backView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.55];
        UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchBackView:)];
        [_backView addGestureRecognizer:tapGesture];
    }
    return _backView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
